package com.dodock.generic.helper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class ProthomAloAndroidFeedGenerator {

	StringBuffer outputbuffer;
	HashMap<String, ArrayList<HashMap<String, String>>> headlines;
	
	public ProthomAloAndroidFeedGenerator(){
		outputbuffer = new StringBuffer();
		headlines = new HashMap<String, ArrayList<HashMap<String, String>>>();
		headlines.put("sirso", new ArrayList<HashMap<String, String>>());
		headlines.put("sorbos", new ArrayList<HashMap<String, String>>());
		headlines.put("noksha", new ArrayList<HashMap<String, String>>());
		headlines.put("anondo", new ArrayList<HashMap<String, String>>());
		headlines.put("roshalo", new ArrayList<HashMap<String, String>>());
		headlines.put("chutirdine", new ArrayList<HashMap<String, String>>());
		headlines.put("khela", new ArrayList<HashMap<String, String>>());
		headlines.put("tech", new ArrayList<HashMap<String, String>>());
		headlines.put("binodon", new ArrayList<HashMap<String, String>>());
		headlines.put("aurtho", new ArrayList<HashMap<String, String>>());
		headlines.put("sompa", new ArrayList<HashMap<String, String>>());
		headlines.put("sespata", new ArrayList<HashMap<String, String>>());
		
	}
	
	private void addExtra(String category_name, String category_id) throws IOException{
		ArrayList<HashMap<String, String>> extranews = ExtraHeadlinesExtractor.getHeadlines("http://m.prothom-alo.com/cat.php?CatID="+category_id);
		for(HashMap<String, String> n:extranews){
			addNewsToCategory(headlines.get(category_name), n);
		}
	}
	
	private void addMainSiteExtra(String category_name, String category_id) throws IOException{
		ArrayList<HashMap<String, String>> extranews = ExtraMainSiteHeadlinesExtractor.getHeadlines("http://www.prothom-alo.com/section/date/0000-00-00/category/"+category_id);
		for(HashMap<String, String> n:extranews){
			addNewsToCategory(headlines.get(category_name), n);
		}
	}
	
	private void addNewsToCategory(ArrayList<HashMap<String, String>> list, HashMap<String, String> news){
		String new_id = news.get("id"); 
				
		for(HashMap<String, String> en : list){
			String old_id = en.get("id");
			if(new_id.equalsIgnoreCase(old_id)){
				return;
			}
		}
		list.add(news);
	}
	
	
	public String generate() throws IOException{
		
		
		Document doc = Jsoup.connect("http://m.prothom-alo.com/").get();
		Elements links = doc.select("a[href]");
		
		String current_cat = null;
		
		for(Element a:links){
			String href = a.attr("href");
			
			
			//outputbuffer.append(href);
			//outputbuffer.append("\n");
			
			if(href.startsWith("/")){
				current_cat = "sirso";
			}
			else if(href.contains("index.php?type")){
				current_cat = "sorbos";
			}
			else if(href.contains("cat.php?CatID=15")){
				current_cat = "khela";
				addExtra(current_cat, "15");
			}
			else if(href.contains("cat.php?CatID=420")){
				current_cat = "binodon";
				addExtra(current_cat, "420");
			}
			else if(href.contains("cat.php?CatID=50")){
				current_cat = "aurtho";
				addExtra(current_cat, "50");
			}
			else if(href.contains("cat.php?CatID=91")){
				current_cat = "sompa";
				addExtra(current_cat, "91");
			}
			else if(href.contains("cat.php?CatID=6")){
				current_cat = "sespata";
				addExtra(current_cat, "6");
			}
			else if(href.contains("details.php?newsId=")){
				String text = a.text();
				int pos = href.indexOf("=");
				String id = href.substring(pos + 1);
				
				HashMap<String, String> news = new HashMap<String, String>();
				news.put("news", text);
				news.put("id", id);
				//headlines.get(current_cat).add(news);
				addNewsToCategory(headlines.get(current_cat), news);
			}
		}
		
		addExtra("tech", "54");
		
		addMainSiteExtra("noksha", "45");
		addMainSiteExtra("anondo", "12");
		addMainSiteExtra("roshalo", "27");
		addMainSiteExtra("chutirdine", "46");
		
		outputCategory("sirso");
		outputCategory("sorbos");
		outputCategory("noksha");
		outputCategory("anondo");
		outputCategory("roshalo");
		outputCategory("chutirdine");
		outputCategory("khela");
		outputCategory("tech");
		outputCategory("binodon");
		outputCategory("aurtho");
		outputCategory("sompa");
		outputCategory("sespata");
		
		/*
		for(String key:headlines.keySet()){
			outputCategory(key);
		}
		*/
		return outputbuffer.toString();
	}
	
	private void outputCategory(String category_key){
		ArrayList<HashMap<String, String>> hs = headlines.get(category_key);
		outputbuffer.append("<");
		outputbuffer.append(category_key);
		outputbuffer.append(">");
		outputbuffer.append("\n");
		
		for(int i=0; i<hs.size(); i++){
			
			HashMap<String, String> news = hs.get(i);
			
			outputbuffer.append("<news>");
			outputbuffer.append(news.get("news"));
			outputbuffer.append("</news>");
			
			outputbuffer.append("<id>");
			outputbuffer.append(news.get("id"));
			outputbuffer.append("</id>");
			
			outputbuffer.append("\n");
		}
		
		
		
		outputbuffer.append("</");
		outputbuffer.append(category_key);
		outputbuffer.append(">");
		outputbuffer.append("\n");
	}
	
	
}
